package edu.guigu.networkdemo.network;

import java.util.List;

import edu.guigu.networkdemo.model.User;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class GithubClient {

    private GithubApi githubApi;
    private static GithubClient githubClient;

    public static GithubClient getInstance(){
        if(githubClient == null){
            githubClient = new GithubClient();
        }
        return githubClient;
    }

    private GithubClient(){

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor) // 添加日志拦截器
                .build();
        // 1
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .client(okHttpClient)   // 添加okhttp(我们的)
                // Retrofit强大的一个功能,可直接添加转换器
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        // 2
        githubApi = retrofit.create(GithubApi.class);
    }


    public Call<User> getUser(String user) {
        return githubApi.getUser(user);
    }

    public Call<List<User>> listUsers() {
        return githubApi.listUsers();
    }
}
